<?php
class Posts extends CActiveRecord {
	/** Post status */
	const STATUS_PUBLISHED = 1;
	const STATUS_FORBIDDEN = 2;
	public $topicId;
	public $categoryId;
	
	
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	public function tableName() {
		return strtolower('Posts');
	}
	
	public function rules() {
		return array(
			array('postContent, postTitle, postStatus', 'required'),
			array('categoryId', 'required', 'on' => array('insert', 'edit')),
			array('postTitle', 'length', 'max' => 255),
			array('postStatus', 'in', 'range' => array(0, 1, 2))
		);
	}
	
	public function safeAtrributes() {
		return array('postTitle', 'postContent', 'postStatus');
	}
	
	public function relations() {
		return array(
			'author' => array(self::BELONGS_TO, 'ForumMembers', 'postAuthor'),
			'comments' => array(self::HAS_MANY, 'Comments', 'postId'),
			'categories' => array(self::MANY_MANY, 'TermsTaxonomy', 'termsrelationships(objectId, subjectId)',
				'together' => true,
//				'with' => 'terms',
				'joinType' => 'LEFT JOIN',
				'condition' => 'taxonomy = "'.TermsTaxonomy::TAXONOMY_CATEGORY.'"'
			),
			'topics' => array(self::MANY_MANY, 'TermsTaxonomy', 'termsrelationships(objectId, subjectId)',
				'together' => true,
//				'with' => 'terms',
				'joinType' => 'LEFT JOIN',
				'condition' => 'taxonomy = "'.TermsTaxonomy::TAXONOMY_TOPIC.'"'
			),
			'tags' => array(self::MANY_MANY, 'TermsTaxonomy', 'termsrelationships(objectId, subjectId)',
				'together' => true,
				'joinType' => 'INNER JOIN'
			)
		);
	}
	
	public function getStatusOptions() {
		return array(
			self::STATUS_PUBLISHED => '已发布',
			self::STATUS_FORBIDDEN  => '屏蔽'
		);
	}
	
	public function scopes() {
		return array(
			'published' => array(
				'condition' => 'postStatus = '.Posts::STATUS_PUBLISHED
			),
			'section' => array(
				'limit' => '10',
				'order' => 'postId DESC'
			)
		);
	}
	
	public function attributeLabels() {
		return array(
			'postContent' => '文章内容',
			'postTitle' => '文章标题',
			'categoryId' => '分类'
		);
	}
	
	public function getStatusText() {
		$options = $this->statusOptions;
		return isset($options[$this->postStatus]) ? $options[$this->postStatus] : "未知 {$this->postStatus}";
	}
	
	public function findByCategoryId($termTaxonomyId, $mergeCriteria = null) {
		$criteria = new CDbCriteria;
		if($mergeCriteria !== null) {
			$criteria->mergeWith($mergeCriteria);
		}
		$criteria->condition = 'termTaxonomyId IN ('.$termTaxonomyId.')';
		return $this->with('categories')->findAll($criteria);
	}
	
	public function findByTopicId($termTaxonomyId, $mergeCriteria = null) {
		$criteria = new CDbCriteria;
		if($mergeCriteria !== null) {
			$criteria->mergeWith($mergeCriteria);
		}
		$criteria->condition = 'termTaxonomyId IN ('.$termTaxonomyId.')';
		return $this->with('topics')->findAll($criteria);
	}
	
	public function hasTopic() {
		return isset($this->topics) && sizeof($this->topics) > 0;
	}
	
	public function getTopicName() {
		return $this->hasTopic() ? $this->topics[0]->terms->name : '无';
	}
	
	public function findBySlug($slug) {
		return Posts::model()->with('categories')->find('postSlug = ?', array($slug));
	}
	
	public function beforeSave() {
		$this->postSlug = preg_replace('/[-_\&\?\s\:\^\%\$\@\!\(\)\"\'\;\.\,\<\>\/\\\+\=\{\}\[\]\|]/', '-', $this->postTitle);
		return true;
	}

	public function getRewrittenUrl() {
//		return CHtml::normalizeUrl(array('/posts/view', 'id' => $this->postId));
//		return Yii::app()->baseUrl.'/'.urlencode($this->postSlug).'.html';
		return Yii::app()->baseUrl.'/'.$this->postSlug.'.html';
	}
}
